﻿#include "newuserrelationwidget.h"
#include "ui_newuserrelationwidget.h"

NewUserRelationWidget::NewUserRelationWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewUserRelationWidget)
{
    ui->setupUi(this);

    setTabOrder(ui->lineEditQueryUsername, ui->lineEditQueryCompanyId);

    setTabOrder(ui->lineEditNonPlatformNickName, ui->lineEditNonPlatformContact);
    setTabOrder(ui->lineEditNonPlatformContact, ui->lineEditNonPlatformTel);
    setTabOrder(ui->lineEditNonPlatformTel, ui->lineEditNonPlatformRemark);
    setTabOrder(ui->lineEditNonPlatformRemark, ui->comboBoxPrimaryDirectory);
    setTabOrder(ui->comboBoxPrimaryDirectory, ui->comboBoxSecondaryDirectory);

    connect(&userRelationService, &UserRelationService::queryPlatformUserSuccess,
            [=](User &user, Company &company)
    {
        this->targetUsername = user.username;

        ui->lineEditUserNickName->setText(user.nickName);
        ui->lineEditUserPosition->setText(user.position);
        ui->lineEditUserTel->setText(user.tel);
        ui->lineEditUserMobile->setText(user.mobile);
        ui->lineEditUserQQ->setText(user.QQ);
        ui->lineEditUserWechat->setText(user.wechat);

        ui->lineEditCompanyName->setText(company.companyName);
        ui->lineEditContact->setText(company.contact);
        ui->lineEditFax->setText(company.fax);
        ui->lineEditTel->setText(company.tel);
        ui->lineEditWebsite->setText(company.website);
        ui->lineEditEmail->setText(company.email);
        ui->textEditIntroduction->setText(company.introduction);

        ui->pushButtonPlatformAdd->setEnabled(true);
    });

    connect(&userRelationService, &UserRelationService::addPlatformUserFailed,
            [=]()
    {
        ui->pushButtonPlatformAdd->setEnabled(true);
    });
}

NewUserRelationWidget::~NewUserRelationWidget()
{
    delete ui;
}

void NewUserRelationWidget::showAndClear()
{
    ui->lineEditQueryUsername->clear();
    ui->lineEditQueryCompanyId->clear();
    ui->lineEditUserNickName->clear();
    ui->lineEditUserPosition->clear();
    ui->lineEditUserTel->clear();
    ui->lineEditUserMobile->clear();
    ui->lineEditUserQQ->clear();
    ui->lineEditUserWechat->clear();

    ui->lineEditCompanyName->clear();
    ui->lineEditContact->clear();
    ui->lineEditTel->clear();
    ui->lineEditFax->clear();
    ui->lineEditWebsite->clear();
    ui->lineEditEmail->clear();
    ui->textEditIntroduction->clear();

    this->targetUsername.clear();
    ui->pushButtonPlatformAdd->setEnabled(false);

    ui->lineEditNonPlatformNickName->clear();
    ui->lineEditNonPlatformContact->clear();
    ui->lineEditNonPlatformRemark->clear();
    ui->lineEditNonPlatformTel->clear();

    exec();
}

void NewUserRelationWidget::setWidgetType(UserRelation::RelationType widgetType)
{
    this->widgetType = widgetType;
    if(widgetType == UserRelation::Client){
        setWindowTitle("添加客户");
    }else{
        setWindowTitle("添加供应商");
    }
}

void NewUserRelationWidget::setDirectory(QStringList primaryDirectoryList, QList<QStringList> secondaryDirectoryList)
{
    this->primaryDirectoryList = primaryDirectoryList;
    this->secondaryDirectoryList = secondaryDirectoryList;
    ui->comboBoxPrimaryDirectory->clear();
    ui->comboBoxPrimaryDirectory->addItems(primaryDirectoryList);
}

void NewUserRelationWidget::setCurrentDirectory(QString primaryDirectory, QString secondaryDirectory)
{
    ui->comboBoxPrimaryDirectory->setCurrentText(primaryDirectory);
    ui->comboBoxSecondaryDirectory->setCurrentText(secondaryDirectory);
}

void NewUserRelationWidget::on_pushButtonPlatformCancel_clicked()
{
    this->close();
}

void NewUserRelationWidget::on_pushButtonPlatformAdd_clicked()
{
    ui->pushButtonPlatformAdd->setEnabled(false);
    userRelationService.addPlatformUser(this, ui->pushButtonPlatformAdd, targetUsername, widgetType);
}

void NewUserRelationWidget::on_pushButtonQuery_clicked()
{
    userRelationService.queryPlatformUser(this, ui->lineEditQueryUsername->text(), ui->lineEditQueryCompanyId->text());
}

void NewUserRelationWidget::on_pushButtonNonPlatformCancel_clicked()
{
    this->close();
}

void NewUserRelationWidget::on_pushButtonNonPlatformAdd_clicked()
{
    userRelationService.addNonPlatformUser(
                this,
                ui->pushButtonNonPlatformAdd,
                widgetType,
                ui->lineEditNonPlatformNickName->text(),
                ui->lineEditNonPlatformContact->text(),
                ui->lineEditNonPlatformTel->text(),
                ui->lineEditNonPlatformRemark->text(),
                ui->comboBoxPrimaryDirectory->currentText(),
                ui->comboBoxSecondaryDirectory->currentText());
}

void NewUserRelationWidget::on_comboBoxPrimaryDirectory_currentIndexChanged(int index)
{
    if(index != -1){
        ui->comboBoxSecondaryDirectory->clear();
        ui->comboBoxSecondaryDirectory->addItems(this->secondaryDirectoryList[index]);
    }
}
